<template>
  <div v-loading="loading" class="app-main-content-box">
    <!-- 新增服务 -->
    <el-button type="primary" size="medium" round @click="add">新增服务</el-button>

    <!-- 表格 start-->
    <div class="content-container">
      <el-table
        :data="list"
        border
        stripe
        style="width: 100%"
        size="small"
        element-loading-text="加载中"
        empty-text="暂无数据"
      >
        <el-table-column prop="title" label="服务名称" width="160" />
        <el-table-column prop="description" label="服务描述" />
        <el-table-column prop="defaulted" label="是否默认服务" width="120">
          <template slot-scope="scope">{{ scope.row.defaulted?"是":"否" }}</template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="edit(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 表格 end-->

    <!-- 组件 -->
    <DialogEditSupport ref="dialogEditSupport" />
  </div>
</template>

<script>
import DialogEditSupport from './DialogEditSupport'
import { supportList, delSupport } from '@/views/wxaMallModule/api/support'
export default {
  components: {
    DialogEditSupport
  },
  data() {
    return {
      // 配置
      // 状态
      loading: false,
      // 数据
      list: []
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    // 获取数据
    this.fetchData()
  },
  methods: {
    /**
       * 数据组
       */
    // 获取列表
    fetchData() {
      this.loading = true
      supportList({ type: 1 })
        .then((res) => {
          this.list = res.data
        })
        .finally(() => {
          this.loading = false
        })
    },

    /**
       * 按钮组
       */
    // 新增
    add() {
      this.$refs.dialogEditSupport.open({ mode: 'add' }).then((res) => {
        this.fetchData()
      })
    },

    // 编辑
    edit(row) {
      this.$refs.dialogEditSupport.open({ mode: 'edit', row }).then((res) => {
        this.fetchData()
      })
    },

    // 删除
    del(row) {
      this.$confirm(`确定删除服务：${row.title}?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true,
        roundButton: true
      })
        .then(() => {
          this.loading = true
          delSupport({ supportId: row.id })
            .then((res) => {
              this.$notify({
                title: '成功',
                message: '删除成功',
                type: 'success',
                duration: 2000,
                offset: 40
              })
              this.fetchData()
            })
            .catch((err) => {})
            .finally(() => {
              this.loading = false
            })
        })
        .catch(() => {})
    }
    /**
       * 状态组
       */
    /**
       * 方法组
       */
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  .content-container {
    margin-top: 20px;
  }
</style>
